package com.ws.shop.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Letter;
import com.ws.shop.service.LetterService;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/letter/*")
public class LetterAction extends BaseAdminAction{
	private static Logger log = Logger.getLogger(LetterAction.class);
	@Autowired
	private LetterService letterService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = letterService.findLetterListPage(pageno,0);
		model.addAttribute(Page.PAGE, page);
		return "letter_index";
	}

	public String send(){
		return "letter_send";
	}
	
	public String insert(HttpServletRequest request,HttpServletResponse response){
		String receptionname = RequestUtil.getStringParameter(request, "receptionname", null);
		int state = RequestUtil.getIntParameter(request, "state", 0);
		String message = RequestUtil.getStringParameter(request, "message", null);
		if(message == null)
			return this.ajaxJsonErrorMessage(response, "消息内容不空");
		
		String sendname = null;
		Letter letter = new Letter(0,sendname,receptionname,message,0,null,state);
		try{
			if(state == 1){
				letterService.sendAllLetter(letter);
			}else{
				if(receptionname == null){
					return this.ajaxJsonErrorMessage(response, "非全局广播请输入用户昵称");
				}
				letterService.sendLetter(letter);
			}
			return this.ajaxJsonSuccessMessage(response, "发送成功");
		}catch(Exception e){
			log.error("发送失败",e);
			return this.ajaxJsonErrorMessage(response, "发送失败，请稍后再试");
		}
	}

}
